package TestCases;

import static org.junit.Assert.*;

import java.util.ArrayList;

import junit.framework.TestCase;
import local_database.*;

import org.junit.Test;

public class XmlTest extends TestCase {

	@Test
	// creating Root successfully
	public void testForXmlWriter1() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.createRoot();
		assertEquals(result, true);
	}

	@Test
	// Adding new Element successfully
	public void testForXmlWriter2() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElementToRoot("Currency","code","JPY");
		result = writer.AddNewElementToRoot("Currency","code","HRK");
		assertEquals(result, true);
	}
	
	@Test
	// Adding an existed Element
	public void testForXmlWriter2_1() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElementToRoot("Currency","code","JPY");
		assertEquals(result, false);
	}
	
	@Test
	// Adding an non-existed Element
	public void testForXmlWriter2_2() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElementToRoot("Currency","code","GBP");
		assertEquals(result, true);
	}
	
	@Test
	// Adding new Element to specific tag
	public void testForXmlWriter3() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElement("Currency", "buy_price","5.6");
		result = writer.AddNewElement("Currency", "buy_time", "2-9-2013 13:05");
		assertEquals(result, true);
	}
	
	@Test
	// Adding new Element to specific tag with identified attribute
	public void testForXmlWriter4() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElement("Currency", "code","HRK","sell_price","6.6");
		result = writer.AddNewElement("Currency", "code","HRK","sell_time","4-9-2013 13:06");
		result = writer.AddNewElement("Currency", "code","HRK","sell_price","6.9");
		result = writer.AddNewElement("Currency", "code","HRK","buy_price","7.9");
		result = writer.AddNewElement("Currency", "code","JPY","buy_price","0.07");
		assertEquals(result, true);
	}
	
	@Test
	// Adding an existed Element
	public void testForXmlWriter2_3() {
		boolean result;
		XmlWriter writer = new XmlWriter("UserStat.xml");
		result = writer.AddNewElementToRoot("Currency","code","JPY");
		assertEquals(result, false);
	}

	@Test
	// Read the root node of xml
	public void testForXmlReader1() {
		String result;
		XmlReader reader = new XmlReader("UserStat.xml");
		result = reader.getRootnode();
		assertEquals(result, "Statistic");
	}

	@Test
	// Get Node value using node name
	public void testForXmlReader2() {
		String result;
		XmlReader reader = new XmlReader("UserStat.xml");
		result = reader.getNodeAttrContent("Currency","code");
		assertEquals(result, "JPY");
	}

	@Test
	// Fail to get Node value using invalid node name
	public void testForXmlReader3() {
		String result;
		XmlReader reader = new XmlReader("UserStat.xml");
		result = reader.getNodeTextContent("Time");
		assertEquals(result, "N/A");
	}

	@Test
	// Get Node values using node name
	public void testForXmlReader4() {
		String[] result;
		ArrayList resultList;
		XmlReader reader = new XmlReader("UserStat.xml");
		resultList = reader.getNodesAttrContent("Currency","code");
		result = new String[resultList.size()];
		for (int i = 0; i < resultList.size(); i++) {
			result[i] = (String) resultList.get(i);
		}
		
		assertEquals(result[0],"JPY");
		assertEquals(result[1],"HRK");
	}
	
	@Test
	// Fail to get Node value using invalid node name
	public void testForXmlReader5() {
		String result;
		XmlReader reader = new XmlReader("UserStat.xml");
		result = reader.getNodeTextContent("Currency", "code", "HRK", "sell_price");
		assertEquals(result, "6.9");
	}
	
	@Test
	// Fail to get Node value using invalid node name
	public void testForXmlReader6() {
		ArrayList result = new ArrayList();
		ArrayList expectedResult = new ArrayList();
		expectedResult.add("0.07");
		expectedResult.add("7.9");
		expectedResult.add("5.6");
		XmlReader reader = new XmlReader("UserStat.xml");
		result = reader.getNodesTextContent("buy_price");
		assertEquals(result, expectedResult);
	}
	
	/*
	@Test
	public void testForStat1(){
		Statistic s = new Statistic("UserStat.xml");
		s.process();
	}
	*/

}
